<?php
	
	/**
	  *	This file is responsible for main page representation
	  *
	  *	@author Vytenis Urbonavicius <vytenis.urbonavicius@myhost.lt>
	  *	@version 0.0
	  *	@todo Represent method arguments conveniently
	  *	@todo Collect all constants in one place
	  *	@todo Make diff functionality between versions
	  *	@todo Rebuild this interface with new design
	  *	@todo Put some ifs to avoid displaying empty data, etc...
	  */
	
	/**
	  *	This constant defines security value that does not allow direct access to other scripts
	  *
	  *	@name SECURITY
	  */
	define( 'SECURITY', 1 );
	
	session_start();
	
	include( 'import_list.php' );
	
	$get_type = !empty( $_GET['type'] ) ? $_GET['type'] : null;
	$get_value = !empty( $_GET['value'] ) || ( ( isset( $_GET['value'] ) && ( $_GET['value'] == 0 ) ) ) ? $_GET['value'] : null;
	
	if ( !is_null( $get_type ) ) {
		Retriever::savePath( $get_type, $get_value );
	}
	
	$place_info = Retriever::getPlaceInfo();
	$current_place = $place_info['current'];
	
?>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
	<head>
		<link rel="stylesheet" type="text/css" href="css/main.css" />
		<script type="text/javascript" src="js/comanche_ajax.js"></script>
	</head>
	<body>
		<div style="color: #030; font-weight: bold;">
			<table style="background-color: #DDD; border-collapse: collapse; position: absolute; height: 100%; width: 100%;">
				<tr>
					<td style="border: 1px solid #000000; height: 35px; width: 250px; padding-left: 10px; padding-right: 10px;">
						<div id="ajax_box">
							Waiting for packet selection.
						</div>
						<script type="text/javascript">
						<!--
							<?php
								
								$bad_places = array(
									'root',
								);
								
								if ( !in_array( $current_place, $bad_places ) ) {
									?>
										
										obj_ajax = new comancheAjax();
										
										first_time = true;
										
										function checkAndDo() {
											
											ajax_box = document.getElementById( 'ajax_box' );
											
											if ( ( obj_ajax.getResponse() == '' ) || ( obj_ajax.getResponse() == undefined ) || first_time ) {
												
												ajax_box.innerHTML = 'Renewing...';
												setTimeout( "obj_ajax.sendRequest( 'ajax.php', 'GET', 'project=<?php print( $place_info['packet'] ); ?>' );", 100 );
												setTimeout( "ajax_box.innerHTML = 'Renewing...'", 100 );
												
											} else if ( obj_ajax.getResponse() == '1' ) {
												
												ajax_box.innerHTML = 'Renewal complete!';
												
											} else {
												
												ajax_box.innerHTML = obj_ajax.getResponse();
												
											}
											
											if ( first_time ) {
												first_time = false;
											}
											
										}
										
										obj_ajax.onAjaxRequestComplete = checkAndDo;
										
										checkAndDo();
										
									<?php
								}
								
							?>
						-->
						</script>
					</td>
					<td style="border: 1px solid #000000; height: 35px; padding-left: 10px; padding-right: 10px;">
						<?php
							
							$path_tree = array(
								'root',
								'packet',
								'version',
								array(
									'todo',
								),
								array(
									'file',
									'define'
								),
								array(
									'class',
									array(
										'method'
									),
									array(
										'property',
									)
								),
							);
							
							function returnArrayUntilMatch( $match_array, $match ) {
								
								$result_array = array();
								
								foreach( $match_array as $item ) {
									
									if ( is_array( $item ) ) {
										
										$result = returnArrayUntilMatch( $item, $match );
										
										if ( !empty( $result ) ) {
											
											foreach( $result as $result_item ) {
												
												$result_array[] = $result_item;
												
											}
											
											return $result_array;
											
										}
										
									} else {
										
										if ( $item == $match ) {
											
											$result_array[] = $item;
											return $result_array;
											
										} else {
											
											$result_array[] = $item;
											
										}
										
									}
									
								}
								
								return false;
								
							}
							
							$path = returnArrayUntilMatch( $path_tree, $current_place );
							
							$first_loop = true;
							
							if ( !empty( $path ) ) {
								foreach( $path as $item ) {
									
									if ( $first_loop ) {
										$first_loop = false;
									} else {
										?>&nbsp;&#62;&nbsp;<?php
									}
									
									$unchanged_items = array(
										'root',
										'todo',
									);
									
									$new_item = null;
									
									if ( !in_array( $item, $unchanged_items ) ) {
										$new_item = $place_info[$item];
										if ( $item == 'version' ) {
											$new_item = 'v.' . $new_item;
										}
									} else {
										$new_item = $item;
									}
									
									?><a href="index.php?type=<?php print( $item ); ?>" style="color: #010;" alt=""><?php print( $new_item ); ?></a><?php
								}
							}
							
						?>
					</td>
				</tr>
				<tr>
					<td style="border: 1px solid #000000; padding-left: 10px; padding-right: 10px; vertical-align: top; padding-top: 3px;">
						<?php
							
							switch( $current_place ) {
								
								case 'root': {
									?>
										<div style="font-size: 12px;">./</div><br />
										Packets:<br /><br />
									<?php
									$packets = Retriever::getPackets();
									sort( $packets );
									$first_time = true;
									if ( !empty( $packets ) ) {
										foreach( $packets as $packet ) {
											if ( $first_time ) {
												$first_time = false;
											} else {
												?><br /><?php
											}
											?>
												<a href="index.php?type=packet&value=<?php print( $packet ); ?>" style="color: #010;" alt=""><?php print( $packet ); ?></a>
											<?php
										}
									}
									break;
								}
								case 'packet': {
									?>
										<a href="index.php?type=root" style="color: #010; text-decoration: none; font-size: 9px;" alt="">&#60; Back</a><br /><br />
										Versions:<br /><br />
									<?php
									$versions = Retriever::getVersions();
									foreach( $versions as $key => $version ) {
										if ( !is_numeric( $version ) ) {
											unset( $versions[$key] );
										} else {
											$versions[$key] = (int)$version;
										}
									}
									sort( $versions );
									
									$first_time = true;
									if ( !empty( $versions ) ) {
										foreach( $versions as $version ) {
											if ( $first_time ) {
												$first_time = false;
											} else {
												?><br /><?php
											}
											?>
												<a href="index.php?type=version&value=<?php print( $version ); ?>" style="color: #010;" alt="">v.<?php print( $version ); ?></a>
											<?php
										}
									}
									break;
								}
								case 'version': {
									?>
										<a href="index.php?type=packet" style="color: #010; text-decoration: none; font-size: 9px;" alt="">&#60; Back</a><br /><br />
										Files:<br /><br />
									<?php
									$files = Retriever::getFiles();
									sort( $files );
									$first_time = true;
									if ( !empty( $files ) ) {
										foreach( $files as $file ) {
											if ( $first_time ) {
												$first_time = false;
											} else {
												?><br /><?php
											}
											?>
												<a href="index.php?type=file&value=<?php print( $file ); ?>" style="color: #010;" alt=""><?php print( $file ); ?></a>
											<?php
										}
									}
									?>
										<br /><br />Classes:<br /><br />
									<?php
									$classes = Retriever::getClasses();
									sort( $classes );
									$first_time = true;
									if ( !empty( $classes ) ) {
										foreach( $classes as $class ) {
											if ( $first_time ) {
												$first_time = false;
											} else {
												?><br /><?php
											}
											?>
												<a href="index.php?type=class&value=<?php print( $class ); ?>" style="color: #010;" alt=""><?php print( $class ); ?></a>
											<?php
										}
									}
									?>
										<br /><br />Other choices:<br /><br />
										<a href="index.php?type=todo" style="color: #010;" alt="">Todo tasks</a>
									<?php
									break;
								}
								case 'file': {
									?>
										<a href="index.php?type=version" style="color: #010; text-decoration: none; font-size: 9px;" alt="">&#60; Back</a><br /><br />
										Defines:<br /><br />
									<?php
									$defines = Retriever::getDefines();
									sort( $defines );
									$first_time = true;
									if ( !empty( $defines ) ) {
										foreach( $defines as $define ) {
											if ( $first_time ) {
												$first_time = false;
											} else {
												?><br /><?php
											}
											?>
												<a href="index.php?type=define&value=<?php print( $define ); ?>" style="color: #010;" alt=""><?php print( $define ); ?></a>
											<?php
										}
									}
									break;
								}
								case 'define': {
									?><a href="index.php?type=file" style="color: #010; text-decoration: none; font-size: 9px;" alt="">&#60; Back</a><?php
									break;
								}
								case 'class': {
									?>
										<a href="index.php?type=version" style="color: #010; text-decoration: none; font-size: 9px;" alt="">&#60; Back</a><br /><br />
										Methods:<br /><br />
									<?php
									$methods = Retriever::getMethods();
									sort( $methods );
									$first_time = true;
									if ( !empty( $methods ) ) {
										foreach( $methods as $method ) {
											if ( $first_time ) {
												$first_time = false;
											} else {
												?><br /><?php
											}
											?>
												<a href="index.php?type=method&value=<?php print( $method ); ?>" style="color: #010;" alt=""><?php print( $method ); ?></a>
											<?php
										}
									}
									?>
										<br /><br />Properties:<br /><br />
									<?php
									$properties = Retriever::getProperties();
									sort( $properties );
									$first_time = true;
									if ( !empty( $properties ) ) {
										foreach( $properties as $property ) {
											if ( $first_time ) {
												$first_time = false;
											} else {
												?><br /><?php
											}
											?>
												<a href="index.php?type=property&value=<?php print( $property ); ?>" style="color: #010;" alt=""><?php print( $property ); ?></a>
											<?php
										}
									}
									break;
								}
								case 'method': {
									?><a href="index.php?type=class" style="color: #010; text-decoration: none; font-size: 9px;" alt="">&#60; Back</a><?php
									break;
								}
								case 'property': {
									?><a href="index.php?type=class" style="color: #010; text-decoration: none; font-size: 9px;" alt="">&#60; Back</a><?php
									break;
								}
								case 'todo': {
									?><a href="index.php?type=version" style="color: #010; text-decoration: none; font-size: 9px;" alt="">&#60; Back</a><?php
									break;
								}
								
							}
							
						?>
					</td>
					<td style="border: 1px solid #000000; padding-left: 40px; padding-right: 10px; padding-top: 30px; vertical-align: top;">
							<?php
							
								switch( $current_place ) {
									
									case 'root': {
										?>
											<div style="font-size: 18px;">
												CMS Documentator v.0 alpha
											</div>
											<div style="padding-left: 50px;">
												made by Vytenis Urbonavicius, 2009
											</div>
											<div style="font-size: 10px; padding-left: 80px;">
												Use this software on your own risk.
											</div>
										<?php
										break;
									}
									case 'packet': {
										$info = Retriever::getPacket();
										?>
											Description:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['description'] ) ) {
														foreach ( $info['desc']['description'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										break;
									}
									case 'version': {
										$info = Retriever::getVersion();
										?>
											Description:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['description'] ) ) {
														foreach ( $info['desc']['description'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										break;
									}
									case 'file': {
										$info = Retriever::getFile();
										?>
											Version:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													print ( $info['desc']['version'] );
												?>
											</div>
										<?php
										?>
											<br />Authors:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['authors'] ) ) {
														foreach ( $info['desc']['authors'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Description:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['description'] ) ) {
														foreach ( $info['desc']['description'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Classes:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['classes'] ) ) {
														foreach ( $info['classes'] as $line ) {
															?><a href="index.php?type=class&value=<?php print ( $line ); ?>" style="color: #010;" alt=""><?php print ( $line ); ?></a><?php
														}
													}
												?>
											</div>
										<?php
										break;
									}
									case 'define': {
										$info = Retriever::getDefine();
										?>
											Description:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['description'] ) ) {
														foreach ( $info['desc']['description'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										break;
									}
									case 'class': {
										$info = Retriever::getClass();
										?>
											Version:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													print ( $info['desc']['version'] );
												?>
											</div>
										<?php
										?>
											<br />Authors:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['authors'] ) ) {
														foreach ( $info['desc']['authors'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Flags:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['rights'] ) ) {
														$rights = explode( ' ', $info['desc']['rights'] );
														foreach ( $rights as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Description:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['description'] ) ) {
														foreach ( $info['desc']['description'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										break;
									}
									case 'method': {
										$info = Retriever::getMethod();
										?>
											Version:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													print ( $info['desc']['version'] );
												?>
											</div>
										<?php
										?>
											<br />Authors:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['authors'] ) ) {
														foreach ( $info['desc']['authors'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Flags:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['rights'] ) ) {
														$rights = explode( ' ', $info['desc']['rights'] );
														foreach ( $rights as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Description:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['description'] ) ) {
														foreach ( $info['desc']['description'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Arguments:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['parameters'] ) ) {
														foreach ( $info['desc']['parameters'] as $key => $line ) {
															?>{<br /><div style="padding-left: 20px;"><?php
															?>Parameter type: <?php print( $line['parameter_type'] ); ?><br />
															Data type: <?php print( $line['data_type'] ); ?><br /><br />
															Description:<br /><br />
															<div style="padding-left: 20px;">
																<?php
																	foreach( $line['description'] as $desc_line ) {
																		print( $desc_line . '<br />' );
																	}
																?>
															</div>
															<?php
															?></div><br />}<?php
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Return:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['return'] ) ) {
														?>Data type: <?php print( $info['desc']['return']['data_type'] ); ?><br /><br />
														Description:<br /><br />
														<div style="padding-left: 20px;">
															<?php
																foreach( $info['desc']['return']['description'] as $desc_line ) {
																	print( $desc_line . '<br />' );
																}
															?>
														</div>
														<?php
													}
												?>
											</div>
										<?php
										break;
									}
									case 'property': {
										$info = Retriever::getProperty();
										?>
											Version:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													print ( $info['desc']['version'] );
												?>
											</div>
										<?php
										?>
											<br />Authors:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['authors'] ) ) {
														foreach ( $info['desc']['authors'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Flags:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['rights'] ) ) {
														$rights = explode( ' ', $info['desc']['rights'] );
														foreach ( $rights as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										?>
											<br />Data type:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													print ( $info['desc']['data_type'] );
												?>
											</div>
										<?php
										?>
											<br />Description:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info['desc']['description'] ) ) {
														foreach ( $info['desc']['description'] as $line ) {
															print ( $line . '<br />' );
														}
													}
												?>
											</div>
										<?php
										break;
									}
									case 'todo': {
										$info = Retriever::getTodos();
										?>
											<br />Todo tasks:<br /><br />
											<div style="padding-left: 20px;">
												<?php
													if ( !empty( $info ) ) {
														foreach ( $info as $line ) {
															list( $file, $todo ) = explode( '|', $line );
															print ( 'Todo: ' . $todo . '<br />' );
															print ( '<div style="font-size: 10px;">In file: ' . $file . '</div><br /><br />' );
														}
													}
												?>
											</div>
										<?php
										break;
									}
									
								}
							?>			
					</td>
				</tr>
			</table>
		</div>
	</body>
</html>